const fs = require('fs')
const axios = require('axios')
const path = require('path')
const request = require('request')
const md5 = require('md5-file')

const targetBaseUrl = 'http://www.jnaw.top/cwbuFile/picc-json';
const stepListFileUrl = '/step_list.json';

let stepList = {};

let mvFile = async _ => {
    let file_mp4 = './ffmpeg/mp4/';
    let file_img = './ffmpeg/img/';
    let StaticAssets = './StaticAssets/';

    let suffix_mp4 = '.mp4';
    let suffix_png = '.png';
    let step = 'Step';

    await axios.get(targetBaseUrl + stepListFileUrl).then(res => stepList = res.data);

    for (let i in stepList.steps) {
        let item = stepList.steps[i];
        let name = step + item.name;

        let ARanimation = item.ARanimation || {};
        let ARanimation_url = ARanimation.url || '';
        let ARanimation_splash = ARanimation.splash || '';

        await exitsFolder(StaticAssets + name);
        if (ARanimation_url != '') {
            // 判断文件是否存在
            if (fs.existsSync(file_mp4 + ARanimation_url + suffix_mp4)) {
                copyFile(file_mp4 + ARanimation_url + suffix_mp4, StaticAssets + name + '/' + ARanimation_url + suffix_mp4);
                console.log('复制文件成功>>>>>>>>>>>>>>>>>>>>> ' + name + ' --- ' + file_mp4 + ARanimation_url + suffix_mp4);
            } else {
                console.log('文件不存在：' + file_mp4 + ARanimation_url + suffix_mp4);
            }
        }
        if (ARanimation_splash != '') {
            if (fs.existsSync(file_img + ARanimation_splash + suffix_png)) {
                copyFile(file_img + ARanimation_splash + suffix_png, StaticAssets + name + '/' + ARanimation_splash + suffix_png);
                console.log('复制文件成功>>>>>>>>>>>>>>>>>>>>> ' + name + ' --- ' + file_img + ARanimation_splash + suffix_png);
            } else {
                console.log('文件不存在：' + file_img + ARanimation_splash + suffix_png);
            }
        }

        let realVideo = item.realVideo || {};
        let realVideo_url = realVideo.url || '';
        let realVideo_splash = realVideo.splash || '';
        if (realVideo_url != '') {
            // 判断文件是否存在
            if (fs.existsSync(file_mp4 + realVideo_url + suffix_mp4)) {
                copyFile(file_mp4 + realVideo_url + suffix_mp4, StaticAssets + name + '/' + realVideo_url + suffix_mp4);
                console.log('复制文件成功>>>>>>>>>>>>>>>>>>>>> ' + name + ' --- ' + file_mp4 + realVideo_url + suffix_mp4);
            } else {
                console.log('文件不存在：' + file_mp4 + realVideo_url + suffix_mp4);
            }
        }
        if (realVideo_splash != '') {
            if (fs.existsSync(file_img + realVideo_splash + suffix_png)) {
                copyFile(file_img + realVideo_splash + suffix_png, StaticAssets + name + '/' + realVideo_splash + suffix_png);
                console.log('复制文件成功>>>>>>>>>>>>>>>>>>>>> ' + name + ' --- ' + file_img + realVideo_splash + suffix_png);
            } else {
                console.log('文件不存在：' + file_img + realVideo_splash + suffix_png);
            }
        }

    }
}

// 复制文件
let copyFile = function copyIt(from, to) {
    fs.writeFileSync(to, fs.readFileSync(from));
    //fs.createReadStream(src).pipe(fs.createWriteStream(dst));大文件复制
}

// 创建目录
let exitsFolder = async function (reaPath) {
    if (!fs.existsSync(reaPath)) {
        fs.mkdirSync(reaPath);
    }
}


mvFile();
